﻿using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.ServiceProcess;
using System.Linq;
using System.Windows.Forms;
using ServiceUtilities;

namespace ExcelRefreshService
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();

            serviceInstaller1.SetRecoveryOptions(
                new TimeSpan(1,0,0),                // reset recovery errors after 1 hour
                string.Empty,                       // no command 
                string.Empty,                       // no reboot message
                FailureAction.RestartFifteenSeconds,
                FailureAction.RestartFifteenSeconds,
                FailureAction.RestartFifteenSeconds
                );

            serviceInstaller1.StartOnInstall();
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            var dlg = new DefineCredentials();
            if (dlg.ShowDialog() != DialogResult.OK)
                throw new Exception("Credentials not provided");

            serviceProcessInstaller1.Account = dlg.ServiceAccount;
            serviceProcessInstaller1.Username = dlg.UserName;
            serviceProcessInstaller1.Password = dlg.Password;

            base.Install(stateSaver);
        }
    }
}
